package com.glory.dgcpcpda.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.PopupWindow.OnDismissListener;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener;
import com.pda.platform.ui.ui_pdaplatform.R.id;
import com.pda.platform.ui.ui_pdaplatform.R.layout;
import com.pda.platform.ui.ui_pdaplatform.adapter.FreeUI_NormalPopListAdapter;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextAndPositionCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import java.util.List;

public class FreeApi_PopUtils {
    public FreeApi_PopUtils() {
    }

    public static FreeUI_GeneralPop getNormalPop(Context context, int width, List<String> list, final ImageView ivArrow, final FreeUI_TextCallback editTextScanCallback) {
        View mView = LayoutInflater.from(context).inflate(layout.free_ui_pop_choose_list, (ViewGroup)null);
        RecyclerView recyclerView = (RecyclerView)mView.findViewById(id.rvList);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        if (FreeUI_NormalPopListAdapter.getResID() != layout.free_ui_blue_sky_normal_pop_list) {
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.addItemDecoration(new DividerItemDecoration(context, 1));
        }

        final FreeUI_GeneralPop generalPop = new FreeUI_GeneralPop(context, mView, width, -2);
        FreeUI_NormalPopListAdapter popListAdapter = new FreeUI_NormalPopListAdapter(list);
        popListAdapter.openLoadAnimation(1);
        popListAdapter.isFirstOnly(true);
        popListAdapter.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                editTextScanCallback.onSuccess(String.valueOf(adapter.getItem(position)));
                generalPop.dismiss();
            }
        });
        recyclerView.setAdapter(popListAdapter);
        generalPop.setOutsideTouchable(true);
        generalPop.setOnDismissListener(new OnDismissListener() {
            public void onDismiss() {
                FreeApi_PopUtils.setArrowMiss(ivArrow);
            }
        });
        return generalPop;
    }

    public static FreeUI_GeneralPop getNormalPop(Context context, int width, List<String> list, final ImageView ivArrow, final FreeUI_TextAndPositionCallback textAndPositionCallback) {
        View mView = LayoutInflater.from(context).inflate(layout.free_ui_pop_choose_list, (ViewGroup)null);
        RecyclerView recyclerView = (RecyclerView)mView.findViewById(id.rvList);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        if (FreeUI_NormalPopListAdapter.getResID() != layout.free_ui_blue_sky_normal_pop_list) {
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.addItemDecoration(new DividerItemDecoration(context, 1));
        }

        final FreeUI_GeneralPop generalPop = new FreeUI_GeneralPop(context, mView, width, -2);
        FreeUI_NormalPopListAdapter popListAdapter = new FreeUI_NormalPopListAdapter(list);
        popListAdapter.openLoadAnimation(1);
        popListAdapter.isFirstOnly(true);
        popListAdapter.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                textAndPositionCallback.onSuccess(String.valueOf(adapter.getItem(position)), position);
                generalPop.dismiss();
            }
        });
        recyclerView.setAdapter(popListAdapter);
        generalPop.setOutsideTouchable(true);
        generalPop.setOnDismissListener(new OnDismissListener() {
            public void onDismiss() {
                FreeApi_PopUtils.setArrowMiss(ivArrow);
            }
        });
        return generalPop;
    }

    public static void setArrowMiss(View view) {
        RotateAnimation animationDismiss = new RotateAnimation(180.0F, 0.0F, 1, 0.5F, 1, 0.5F);
        animationDismiss.setDuration(500L);
        animationDismiss.setFillAfter(true);
        view.startAnimation(animationDismiss);
    }

    public static void setArrowShow(View view) {
        RotateAnimation animation = new RotateAnimation(0.0F, 180.0F, 1, 0.5F, 1, 0.5F);
        animation.setDuration(500L);
        animation.setFillAfter(true);
        view.startAnimation(animation);
    }
}
